$(document).ready(v=>{
    getCode()
})
let wxPayData = {
    appidsGloble:'',
    access_tokenGloble:'',
    expires_inGloble:'',
    refresh_tokenGloble:'',
    openidGloble:'',
    scopeGloble:'',
    codeGloble:'',
    prepay_idGloble:'',
    wxSignGloble:'',
    nonceStrGloble:createNonceStr()
}
let wxdata = {
    appId:'',
    timeStamp:'',
    nonceStr:'',
    package:'',
    signType:'',
    paySign:''   
 }
 //元->分
 let yuanToFen = (item)=>{
    var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
    if(reg.test(item)){
        money = parseFloat(item).toFixed(2)*100
        money = money.toFixed(0)
        return money
    }else{
        $.toptip('您输入的金额格式有误', 2000, 'warning')
    }
}
let getUrlParms = (name)=>{
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null)
        return unescape(r[2]);
        return null;
}
function createTimeStamp() {
    var timestamp1 = Date.parse( new Date());
    return timestamp1;
} 
function createNonceStr() {  
    return Math.random().toString(36).substr(2, 15);  
};  

let getToken = (wxcodes)=>{

    if(wxcodes == null){
       $.toptip('网络繁忙，错误码034', 2000, 'warning')
        return;
    }
      $.ajax({
        url: 'http://www.ubitcar.com/app/astokenOpid',
        dataType: 'json',
        type: 'post',
        ContentType: "application/json",
        data: {
            code:wxcodes
        },
        success: function (res) {
            $.hideLoading()
            var obj = eval(res)
            var dataObj = JSON.parse(obj.data)
            if(dataObj.errcode == 40163){
               $.toptip('请从新进入此页面', 2000, 'warning')
                window.location = 'http://www.ubitcar.com'
                return;
            }
            wxPayData.access_tokenGloble = dataObj.access_token
            wxPayData.expires_inGloble = dataObj.expires_in
            wxPayData.refresh_tokenGloble = dataObj.refresh_token
            wxPayData.openidGloble = dataObj.openid
            wxPayData.scopeGloble = dataObj.scope
        },
        error:function(err){
           $.toptip('网络繁忙，错误码035', 2000, 'warning')
           $.hideLoading()
        }
    });
}
function getCode(){
    $.showLoading("加载中...")
    wxPayData.codeGloble = getUrlParms("code")
    getToken(wxPayData.codeGloble)
}
let wxOrder = ()=>{
    $.showLoading("加载中...")
    let _this = this;  
    let jine = $('#money').val()
    let money =  yuanToFen(money)
    alert(money)
    $.ajax({  
        url: 'http://www.ubitcar.com/app/wxPayOrder',  
        dataType: 'json',  
        type: 'post',  
        ContentType: "application/json",  
        data: {
            attach:'沈阳众志成',  
            body: '购买会员',// 支付详情  
            mch_id:'1321518401',
            openid:wxPayData.openidGloble,
            total_fee:money,
            notify_url:'http://www.ubitcar.com/app/wxNoctice'
        },  
        success:function(data) {
            if(data.code == 200){
                wxPayData.prepay_idGloble = data.prepay_id
                wxPaysignFunc()
            }else{
                $.hideLoading()
               $.toptip('网络繁忙，错误码038', 2000, 'warning')
            }  
        },  
        error:function(err){
            $.hideLoading()
           $.toptip('网络繁忙，错误码037', 2000, 'warning')
        }
    });  
}
let wxPaysignFunc = ()=>{
    $.ajax({  
        url: 'http://www.ubitcar.com/app/wxpaysign_api',  
        dataType: 'json',  
        type: 'post',  
        ContentType: "application/json",  
        data: null,  
        success:function(data) {
            if(data.code == 200){
                wxPayData.wxSignGloble = data.data
                //$.toast('微信签名接口返回成功')
                $.hideLoading()
                onBridgeReady()
            }  
        },  
        error:function(err){
            $.hideLoading()
           $.toptip('网络繁忙，错误码039', 2000, 'warning')
        }
    });  
}
function onBridgeReady(){
    WeixinJSBridge.invoke(
        'getBrandWCPayRequest', {
            "appId":wxPayData.wxSignGloble.appId,     //公众号名称，由商户传入     
            "timeStamp":wxPayData.wxSignGloble.timeStamp,         //时间戳，自1970年以来的秒数     
            "nonceStr":wxPayData.wxSignGloble.nonceStr, //随机串     
            "package":wxPayData.wxSignGloble.package,     
            "signType":wxPayData.wxSignGloble.signType,         //微信签名方式：     
            "paySign":wxPayData.wxSignGloble.signed //微信签名 
        },
        function(res){
            if(res.err_msg == "get_brand_wcpay_request:ok" ) {
                window.location = 'http://www.ubitcar.com/app/success.html'
            }else{
                window.location = 'http://www.ubitcar.com/app/error.html'                           
            }     // 使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回    ok，但并不保证它绝对可靠。 
        }
    ) 
    }